import json

rsp_method_error = {'code': '400001', 'msg': 'The method of request must be POST.'}

SYSTEM_ERROR = {
    "code": "999999",
    "success": False,
    "msg": "System Error"
}

RESPONSE_FORMAT_ERROR = {
    "code": "010000",
    "success": False,
    "msg": "response格式不符合标准"
}


def response_format(response, msg=None):
    if not msg:
        return response
    if response['msg']:
        response['msg'] = msg
        return response
    else:
        return RESPONSE_FORMAT_ERROR


KEY_MISS = {
    "code": "100100",
    "success": False,
    "msg": "请求数据非法"
}

SUCCESS_ADD = {
    "code": "000000",
    "success": True,
    "msg": "新增保存成功"
}

SUCCESS_UPDATE = {
    "code": "000000",
    "success": True,
    "msg": "更新保存成功"
}

SUCCESS_INFO = {
    "code": "000000",
    "success": True,
    "msg": "获取信息成功"
}

SUCCESS_SEARCH = {
    "code": "000000",
    "success": True,
    "msg": "查询成功"
}

SUCCESS_LIST = {
    "code": "000000",
    "success": True,
    "msg": "查询成功"
}

SUCCESS_DEL = {
    "code": "000000",
    "success": True,
    "msg": "删除成功"
}


VERIFY_FIELDS_HINT = {
    "code": "010001",
    "success": False,
    "msg": "必填字段为空"
}


LOGIN_FAILED = {
    "code": "100001",
    "success": False,
    "msg": "登录失败"
}

LOGIN_FAILED_LDAP = {
    "code": "100001",
    "success": False,
    "msg": "LDAP还未实现，请选择普通登录"
}

LOGIN_SUCCESS = {
    "code": "000000",
    "success": True,
    "msg": "登录成功",
}

LOGOUT_SUCCESS = {
    "code": "000000",
    "success": True,
    "msg": "登出成功"
}

AUTH_USER_NOT_EXISTS = {
    "code": "999998",
    "success": False,
    "msg": "登陆超时，请重新登陆"
}

AUTH_TOKEN_TIMEOUT = {
    "code": "999997",
    "success": False,
    "msg": "用户未认证"
}

MOCK_NAME_EXISTS = {
    "code": "100001",
    "success": False,
    "msg": "该Mock名称已存在，请重新命名"
}

MOCK_REQUEST_EXISTS = {
    "code": "100001",
    "success": False,
    "msg": "该Mock接口已存在，url和method重复，请确认"
}

AUTH_EXISTS = {
    "code": "100001",
    "success": False,
    "msg": "该项目下此认证信息名称已存在，请重新命名"
}

MODULE_EXISTS = {
    "code": "100101",
    "success": False,
    "msg": "该项目下已存在该模块，请重新命名"
}

PROJECT_EXISTS = {
    "code": "100101",
    "success": False,
    "msg": "该项目已存在"
}

LOG_NOT_EXISTS = {
    "code": "100102",
    "success": False,
    "msg": "日志文件不存在"
}

PROJECT_NOT_EXISTS = {
    "code": "100102",
    "success": False,
    "msg": "项目不存在"
}

DEBUGTALK_NOT_EXISTS = {
    "code": "100102",
    "success": False,
    "msg": "miss debugtalk"
}

DEBUGTALK_UPDATE_SUCCESS = {
    "code": "0002",
    "success": True,
    "msg": "debugtalk更新成功"
}

PROJECT_UPDATE_SUCCESS = {
    "code": "0002",
    "success": True,
    "msg": "项目更新成功"
}

PROJECT_DELETE_SUCCESS = {
    "code": "100003",
    "success": True,
    "msg": "项目删除成功"
}

SYSTEM_ERROR = {
    "code": "999999",
    "success": False,
    "msg": "System Error"
}


KEY_MISS = {
    "code": "000100",
    "success": False,
    "msg": "请求数据非法"
}

FILE_UPLOAD_SUCCESS = {
    'code': '000000',
    'success': True,
    'msg': '文件上传成功'
}

FILE_UPLOAD_FAILED = {
    'code': '100103',
    'success': False,
    'msg': '文件上传失败'
}

FILE_EXISTS = {
    'code': '100101',
    'success': False,
    'msg': '上传文件已存在'
}

FILE_NOT_EXISTS = {
    'code': '0102',
    'success': False,
    'msg': '请求的文件不存在，请确认后再操作'
}

IMPORT_JSON_SUCCESS = {
    'code': '000000',
    'success': True,
    'msg': '导入JSON文件成功'
}

IMPORT_JSON_FAILED = {
    'code': '400001',
    'success': False,
    'msg': '导入JSON文件失败'
}

DATA_TO_LONG = {
    'code': '400100',
    'success': False,
    'msg': '数据信息过长！'
}

API_NOT_FOUND = {
    'code': '0102',
    'success': False,
    'msg': '未查询到该API'
}

API_DEL_SUCCESS = {
    'code': '0003',
    'success': True,
    'msg': 'API删除成功'
}

REPORT_DEL_SUCCESS = {
    'code': '0003',
    'success': True,
    'msg': '报告删除成功'
}

API_UPDATE_SUCCESS = {
    'code': '0002',
    'success': True,
    'msg': 'API更新成功'
}

SUITE_EXISTS = {
    "code": "0101",
    "success": False,
    "msg": "此项目下已存在该套件,请重新命名"
}

SUITE_ADD_SUCCESS = {
    'code': '0001',
    'success': True,
    'msg': 'Suite添加成功'
}

SUITE_DEL_SUCCESS = {
    'code': '0003',
    'success': True,
    'msg': 'Suite删除成功'
}

CASE_ADD_SUCCESS = {
    'code': '0001',
    'success': True,
    'msg': '用例添加成功'
}

CASE_EXISTS = {
    "code": "0101",
    "success": False,
    "msg": "此项目下已存在该用例,请重新命名"
}

CASE_NOT_EXISTS = {
    "code": "0102",
    "success": False,
    "msg": "此用例集不存在"
}

CASE_DELETE_SUCCESS = {
    "code": "0003",
    "success": True,
    "msg": "用例集删除成功"
}

CASE_UPDATE_SUCCESS = {
    'code': '0002',
    'success': True,
    'msg': '用例集更新成功'
}

CONFIG_EXISTS = {
    "code": "0101",
    "success": False,
    "msg": "该项目下此配置已存在，请重新命名"
}

VARIABLES_EXISTS = {
    "code": "0101",
    "success": False,
    "msg": "此变量已存在，请重新命名"
}

CONFIG_ADD_SUCCESS = {
    'code': '0001',
    'success': True,
    'msg': '环境添加成功'
}

VARIABLES_ADD_SUCCESS = {
    'code': '0001',
    'success': True,
    'msg': '变量添加成功'
}

CONFIG_NOT_EXISTS = {
    "code": "0102",
    "success": False,
    "msg": "指定的环境不存在"
}

REPORT_NOT_EXISTS = {
    "code": "0102",
    "success": False,
    "msg": "该报告文件不存在，请确认"
}

VARIABLES_NOT_EXISTS = {
    "code": "0102",
    "success": False,
    "msg": "指定的全局变量不存在"
}

CONFIG_UPDATE_SUCCESS = {
    "code": "0002",
    "success": True,
    "msg": "环境更新成功"
}

VARIABLES_UPDATE_SUCCESS = {
    "code": "0002",
    "success": True,
    "msg": "全局变量更新成功"
}

TASK_ADD_SUCCESS = {
    "code": "000000",
    "success": True,
    "msg": "定时任务新增成功"
}

TASK_TIME_ILLEGAL = {
    "code": "310101",
    "success": False,
    "msg": "时间表达式非法"
}

TASK_NAME_EMPTY = {
    "code": "310102",
    "success": False,
    "msg": "任务名称为空"
}

TASK_ITEM_EMPTY = {
    "code": "310102",
    "success": False,
    "msg": "任务中测试项为空"
}

TASK_HAS_EXISTS = {
    "code": "310102",
    "success": False,
    "msg": "定时任务已存在"
}

TASK_EMAIL_ILLEGAL = {
    "code": "310102",
    "success": False,
    "msg": "请指定邮件接收人列表"
}

TASK_ENABLE_SUCCESS = {
    "code": "310003",
    "success": True,
    "msg": "任务状态重置成功"
}

TASK_DEL_SUCCESS = {
    "code": "310003",
    "success": True,
    "msg": "任务删除成功"
}

PLAN_DEL_SUCCESS = {
    "code": "0003",
    "success": True,
    "msg": "集成计划删除成功"
}

PLAN_ADD_SUCCESS = {
    "code": "0001",
    "success": True,
    "msg": "计划添加成功"
}

PLAN_KEY_EXIST = {
    "code": "0101",
    "success": False,
    "msg": "该KEY值已存在，请修改KEY值"
}

PLAN_ILLEGAL = {
    "code": "0101",
    "success": False,
    "msg": "提取字段格式错误，请检查"
}

PLAN_UPDATE_SUCCESS = {
    "code": "0002",
    "success": True,
    "msg": "计划更新成功"
}

HOSTIP_EXISTS = {
    "code": "0101",
    "success": False,
    "msg": "此域名已存在，请重新命名"
}

HOSTIP_ADD_SUCCESS = {
    'code': '0001',
    'success': True,
    'msg': '域名添加成功'
}

HOSTIP_NOT_EXISTS = {
    "code": "0102",
    "success": False,
    "msg": "指定的域名不存在"
}

HOSTIP_EXISTS = {
    "code": "0101",
    "success": False,
    "msg": "此域名已存在，请重新命名"
}

HOSTIP_UPDATE_SUCCESS = {
    "code": "0002",
    "success": True,
    "msg": "域名更新成功"
}

HOST_DEL_SUCCESS = {
    'code': '0003',
    'success': True,
    'msg': '域名删除成功'
}

TOKEN_FAILED = {
    'code': '0103',
    'success': False,
    'msg': 'token获取失败'
}

RUN_TEST_EXCEPTION = {
    'code': '600000',
    'success': False,
    'msg': '运行完成'
}

RUN_TEST_SUCCESS = {
    'code': '000000',
    'success': True,
    'msg': '运行完成'
}


class ResponseExceptionEncoder(json.JSONEncoder):
    """
    自定义一个类：用于json转换编码，解决TypeError: Object of type xxx is not JSON serializable这个问题
    """
    def default(self, obj):
        if isinstance(obj, bytes):
            return str(obj, encoding='utf-8')
        elif isinstance(obj, Exception):
            return obj.__str__().encode(encoding='utf-8')   # 如果obj对象是Exception对象，则转换为字符串，且设置编码为：utf-8
        else:
            return super(MyEncoder, self).default(obj)
